
<%@ page import="com.medranocg.gd.Document" %>
<!DOCTYPE html>
<html>
	<head>
		<meta name="layout" content="main">
		<g:set var="entityName" value="${message(code: 'document.label', default: 'Document')}" />
		<title>Buscar Documento</title>
		<script type="text/javascript" src="${resource(dir: 'js', file: 'gd.js')}"></script>
		<script>
			function clearCustomer() {
				$('#customerId').val('')
				$('#customerDescription').val('')
				$('#customer').html('')
				
				return false
			}

		    function previewDocument(documentId) {
		        console.debug(documentId);
		        var baseUrl = '<g:createLink controller="document" action="openFileFromRepository" />';
		        var url = baseUrl + '/' + documentId;
		        var previewer = GD.DocumentPreviewer();
		        previewer.previewDocument(url, 'Documento');
		    }

			function loadDocumentDetail(documentId) {
				$('#detalleDocumentoModal').load("${createLink(controller:'consultaDocumentos', action:'modalShow')}",
						"id=" + documentId)
				$('#detalleDocumentoModal').modal('show')

				return false
			}

			function cambiarPagina(pageOffset) {
				$("#offset").val(pageOffset)
				$("#botonBuscar").click()

				return false
			}

			function prevPagina() {
				var offset = ${params.offset ? Integer.valueOf(params.offset) - 2 : 0}

				$("#offset").val(offset)
				$("#botonBuscar").click()

				return false
			}
		</script>
	</head>
	<body>
		<div class="container">
		<div class="main-panel">
		<div class="panel panel-default">
		<div class="panel-heading"><h2>Buscar Documento</h2></div>
		<div class="panel-body">

		<g:form>
			<div class="form-group">
				<label for="grupoDocumental">Grupo Documental</label>
				<g:select name="grupoDocumental" from="['Grupo Documental 2', 'Grupo Documental 3']" multiple="true" class="form-control" />
			</div>

			<div class="form-group">
				<label for="customer">Cliente</label>
				<input type="hidden" id="customerId" name="customerId" value="${params.customerId}" />
				<input type="hidden" id="customerDescription" name="customerDescription" value="${params.customerDescription}" />
				<div class="row">
				<div class="col-xs-10">
					<div class="input-group">
						<g:if test="${params.customerDescription}">
							<div class="form-control" id="customer">${params.customerDescription}</div>
						</g:if>
						<g:else>
							<div class="form-control" id="customer"><span style="color:#aaa">Cliente</span></div>
						</g:else>
						
						<span class="input-group-btn">
							<a href="#" onclick="return clearCustomer()" class="btn btn-default" title="Borrar"><span class="glyphicon glyphicon-remove"></span></a>
						</span>
					</div>
				</div>
				<div class="col-xs-2">
					<a data-toggle="modal" href="${createLink(controller: 'buscarCliente', action: 'modalIndex')}" data-target="#buscarClienteModal" class="btn btn-default">Buscar Cliente</a>
				</div>
				</div>
			</div>

			<div class="form-group">
				<label for="cajaId">Caja</label>
				<input id="cajaId" type="text" class="form-control" name="cajaId" placeholder="Caja" value="${params.cajaId}" />
			</div>

			<div class="form-group">
				<g:actionSubmit id="botonBuscar" value="Buscar" action="buscar" class="btn btn-primary"/>
			</div>

			<g:if test="${listDocument}">
			<table class="table table-striped">
			<thead>
					<tr>

						<th>Cliente</th>
						<th>Tipo de Documento</th>
						<th>Caja AdeA</th>
						<th>Versi&oacute;n</th>
						<th></th>

					</tr>
				</thead>
				<tbody>
				<g:each in="${listDocument}" status="i" var="document">
					<tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
						<td>${document.customer.idPersona} (${document.customer.name}, CUIT: ${document.customer.cuit})</td>

						<td>${document.documentType.name}</td>

						<td>${document.box.cajaId}</td>

						<td>${document.realVersion}</td>

						<td class="col-md-2">
							<button type="button" class="btn btn-default" onclick="previewDocument('${document.id}')"><span class="glyphicon glyphicon-eye-open"></span></button>
							<a href="#" onclick="return loadDocumentDetail(${document.id})" class="btn btn-default"><span class="glyphicon glyphicon-info-sign"></span></a>
						<%--
							<p>
								<a href="${createLink(controller:'consultaDocumentos', id:document.id)}" class="btn-sm btn-info">Ver</a>
								<a data-toggle="modal" href="${createLink(controller:'consultaDocumentos', action:'modalShow', id:document.id)}" data-target="#detalleDocumentoModal"  class="btn-sm btn-info">Datos</a>
							</p>
 --%>
						</td>
					</tr>
				</g:each>
				</tbody>
			</table>
			<input type="hidden" id="offset" name="offset" />
				<ul class="pagination">
					<li class="${!params.offset || params.offset == '0' ? 'disabled' : ''}"><a href="#" onclick="return prevPagina()">&laquo;</a></li>
					<g:each in="${1..documentInstanceCount / 2 + (documentInstanceCount % 2 > 0 ? 1 : 0)}" var="i">
						<li class="${params.offset && Integer.valueOf(params.offset) / 2 + 1 == i ? 'active' : ''}"><a href="#" onclick="return cambiarPagina(${(i - 1) * 2})">${i}</a></li>
					</g:each>
					<li><a href="#">&raquo;</a></li>
				</ul>
			</g:if>
		</g:form>

		</div>
		</div>
		</div>
		</div>

		<div class="modal fade" id="buscarClienteModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"></div>
		<div class="modal fade" id="detalleDocumentoModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"></div>
	</body>
</html>
